package mhys.View.Teacher;
import mhys.Dao.StuDao;
import mhys.Dao.ClassesDao;
import mhys.Entity.Classes;
import mhys.Entity.Student;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.List;
public class StudentPanel extends JPanel{
    DefaultTableModel model;
    JTable jTable;
    StuDao stuDao = new StuDao();
    ClassesDao classesDao = new ClassesDao();
    public StudentPanel() {
        this.setBackground(Color.WHITE);
        this.setLayout(new BorderLayout(10,10));
        JLabel jLabel = new JLabel("学生姓名:");
        jLabel.setBounds(10,10,120,30);

        JPanel jPanel1 = new JPanel();
        jPanel1.setLayout(null);
        jPanel1.setPreferredSize(new Dimension(100, 60));

        JTextField jTextField = new JTextField();
        jTextField.setBounds(130,12,100,30);

        JButton updateButton = new JButton("修改");
        updateButton.setBounds(240,12,60,30);

        JButton delButton = new JButton("删除");
        delButton.setBounds(310,12,60,30);

        jTextField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                String key = jTextField.getText();
                List<Student> students = stuDao.getStuByName(key);
                if (students != null){
                    initData(students);
                }else{
                    JOptionPane.showMessageDialog(null,"查无此人");
                }
            }
        });

        updateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String[] rowData = new String[jTable.getColumnCount()];
                for (int i = 0; i < jTable.getColumnCount(); i++) {
                    rowData[i] = jTable.getValueAt(jTable.getSelectedRow(), i).toString();
                    System.out.println( rowData[i] );
                }

                final JDialog jDialog = new JDialog(App.jFrame,"修改信息",true);
                jDialog.setSize(300, 360);
                jDialog.setLocationRelativeTo(App.jFrame);
                jDialog.setLayout(null);
                JLabel idJLabel = new JLabel("学生ID：");
                idJLabel.setBounds(10, 20, 80, 25);
                final JTextField idTextField = new JTextField(rowData[0]);
                idTextField.setBounds(90, 20, 150, 25);

                JLabel number = new JLabel("学号：");
                number.setBounds(10, 60, 80, 25);
                final JTextField numberTextField = new JTextField(rowData[1]);
                numberTextField.setBounds(90, 60, 150, 25);

                JLabel nameJLabel = new JLabel("学生姓名：");
                nameJLabel.setBounds(10, 100, 80, 25);
                final JTextField nameTextField = new JTextField(rowData[2]);
                nameTextField.setBounds(90, 100, 150, 25);

                JLabel sexJLabel = new JLabel("性别：");
                sexJLabel.setBounds(10, 140, 80, 25);
                String[] itemSex = {"男","女"};
                final JComboBox<String> jComboBox = new JComboBox<String>(itemSex);
                jComboBox.setBounds(90, 140, 150, 25);
                if ("男".equals(rowData[3])) {
                    jComboBox.setSelectedIndex(0);
                } else {
                    jComboBox.setSelectedIndex(1);
                }

                JLabel classJLabel = new JLabel("班级：");
                classJLabel.setBounds(10, 180, 80, 25);


                final JComboBox<String> classBox = new JComboBox<String>();
                classBox.addItem(rowData[4]);
                List<Classes> classes = classesDao.getClasses();
                for (Classes classes1 : classes) {
                    classBox.addItem(classes1.getC_name());
                }
                classBox.setBounds(90, 180, 150, 25);

                JLabel phoneJLabel = new JLabel("手机号：");
                phoneJLabel.setBounds(10, 220, 80, 25);
                final JTextField phoneTextField = new JTextField(rowData[5]);
                phoneTextField.setBounds(90, 220, 150, 25);

                JButton okButton = new JButton("修改");
                okButton.setBounds(100, 260, 80, 25);



                okButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        int index = jTable.getSelectedRow();
                        if (index != -1) {
                            Integer s_id = (Integer)jTable.getValueAt(index,0);
                            String s_number = numberTextField.getText();
                            String s_name = nameTextField.getText();
                            String sex = (String)jComboBox.getSelectedItem();
                            String classes = (String)classBox.getSelectedItem();
                            String phone = phoneTextField.getText();
                            Student student = new Student(s_id,s_name,s_number,sex,classes,phone);
                            int updateStudent = stuDao.updateStu(student);
                            if (updateStudent == 1){
                                List<Student> students = stuDao.getStudents();
                                JOptionPane.showMessageDialog(null, "修改成功");
                                jDialog.dispose();
                                initData(students);
                                jTable.clearSelection();
                                numberTextField.setText("");
                                nameTextField.setText("");
                                jComboBox.setSelectedItem("男");
                                classBox.setSelectedItem("");
                                phoneTextField.setText("");
                            }
                        }else {
                            JOptionPane.showMessageDialog(null, "请选择需要修改的用户");
                        }

                    }
                });

                jDialog.add(classBox);
                jDialog.add(classJLabel);
                jDialog.add(idJLabel);
                jDialog.add(idTextField);
                jDialog.add(numberTextField);
                jDialog.add(nameJLabel);
                jDialog.add(nameTextField);
                jDialog.add(number);
                jDialog.add(sexJLabel);
                jDialog.add(jComboBox);
                jDialog.add(okButton);
                jDialog.add(phoneJLabel);
                jDialog.add(phoneTextField);
                jDialog.setVisible(true);
            }
        });

        delButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int index = jTable.getSelectedRow();
                if (index != -1) {
                    Object id = jTable.getValueAt(index,0);
                    int message = JOptionPane.showConfirmDialog(null,"是否确定删除","选择一个选项",JOptionPane.YES_NO_CANCEL_OPTION);
                    if (message == JOptionPane.YES_OPTION){
                        int delOrder = stuDao.delStu((Integer)id);
                        if (delOrder == 1){
                            List<Student> students = stuDao.getStudents();
                            JOptionPane.showMessageDialog(null,"删除成功");
                            initData(students);
                        }
                    }else{
                        List<Student> students = stuDao.getStudents();
                        initData(students);
                    }
                }else {
                    JOptionPane.showMessageDialog(null,"请选择删除的地址");
                }
            }
        });


        jPanel1.add(updateButton);
        jPanel1.add(delButton);
//        jPanel1.add(searchButton);
        jPanel1.add(jTextField);
        jPanel1.add(jLabel);


        jLabel.setFont(new Font("微软雅黑", Font.PLAIN, 24));
        String[] header = {"学生ID","学号","学生姓名","性别","班级","手机号"};
        model = new DefaultTableModel(header,0){
            public boolean isCellEditable(int row,int column){
                return false;
            }
        };
        jTable = new JTable(model);
        jTable.getTableHeader().setReorderingAllowed(false);
        jTable.setRowHeight(30);
        JScrollPane jScrollPane = new JScrollPane(jTable);
        List<Student> students = stuDao.getStudents();
        initData(students);
        jScrollPane.getViewport().setBackground(Color.WHITE);
        this.add(jPanel1,BorderLayout.NORTH);
        this.add(jScrollPane,BorderLayout.CENTER);

    }
    public  void initData(List<Student> students) {
        model.getDataVector().clear();
        for (Student student : students) {
            Object[] data = {student.getS_id(),student.getS_number(),student.getS_name(),student.getSex(),student.getClasses(),student.getPhone()};
            model.addRow(data);
        }
        jTable.clearSelection();
    }
}
